//模糊搜索
var storePanel=$('#store-list');
var storeNameInput=$('#store');
var storeListPort=common.getStoreListPort;
var companyPanel=$('#company-list');
var companyNameInput=$('#company');
var companyListPort=common.getCompanyListPort;
utils.searchCompanyName({listPanel:storePanel,upIdKey:"companyId",companyNameInput:storeNameInput,searchIdInput:$("#storeId"),companyListPort:storeListPort});
utils.searchCompanyName({listPanel:companyPanel,sonInput:$("#store"),sonIdKey:"storeId",companyNameInput:companyNameInput,searchIdInput:$("#companyId"),companyListPort:companyListPort});
//提交
var url = common.globalHost + common.registerMorePort;
var sendData={};
var data=common.getLocalData('H5-agentInfo');
if(!data){
    window.location.href="login.html";
}
var $businessCardInput=$("#businessCard");
var $frontPhoto=$("#frontPhoto");
var $backPhoto=$("#backPhoto");
var $uploadBusinessCard=$("#esheyi-upload-businessCard");
var $uploadFrontPhoto=$("#esheyi-upload-frontPhoto");
var $uploadBackPhoto=$("#esheyi-upload-backPhoto");
var $agentId=$('#agentId');
var token=data.token;
var agentId=data.id;
    var businessCard=new registerUpload($uploadBusinessCard,{
        agentId:agentId,
        fileType:"businessCard",
        token:token,
        uploadSuccess:function (resFile) {
            businessCard.uploadNotice.html("上传成功").show().delay(1000).hide(0);
            // businessCard.uploadNotice.removeClass("failed").addClass("success").html("上传成功");
            // resFile=resFile.indexOf('http')>-1?resFile:common.imageHost+resFile;
            $businessCardInput.val(resFile);
        },
        uploadFail:function (data) {
            businessCard.uploadNotice.html("上传失败,"+data.res.msg).show().delay(1000).hide(0);
            // businessCard.uploadNotice.removeClass("success").addClass("failed").html("上传失败,"+data.res.msg);
        },
        deleteImage:function () {
            $businessCardInput.val("");
        }
    });
    var frontPhoto=new registerUpload($uploadFrontPhoto,{
       agentId:agentId,
       fileType:"frontPhoto",
       token:token,
       uploadSuccess:function (resFile) {
            frontPhoto.uploadNotice.html("上传成功").show().delay(1000).hide(0);
           // frontPhoto.uploadNotice.removeClass("failed").addClass("success").html("上传成功");
           // resFile=resFile.indexOf('http')>-1?resFile:common.imageHost+resFile;
           $frontPhoto.val(resFile);
       },
       uploadFail:function (data) {
            frontPhoto.uploadNotice.html("上传失败,"+data.res.msg).show().delay(1000).hide(0);
           // frontPhoto.uploadNotice.removeClass("success").addClass("failed").html("上传失败,"+data.res.msg);
       },
       deleteImage:function () {
           $frontPhoto.val("");
       }
    });
    var backPhoto=new registerUpload($uploadBackPhoto,{
        agentId:agentId,
        fileType:"backPhoto",
        token:token,
        uploadSuccess:function (resFile) {
            backPhoto.uploadNotice.html("上传成功").show().delay(1000).hide(0);
            // backPhoto.uploadNotice.removeClass("failed").addClass("success").html("上传成功");
            // resFile=resFile.indexOf('http')>-1?resFile:common.imageHost+resFile;
            $backPhoto.val(resFile);
        },
        uploadFail:function (data) {
            backPhoto.uploadNotice.html("上传失败,"+data.res.msg).show().delay(1000).hide(0);
            // backPhoto.uploadNotice.removeClass("success").addClass("failed").html("上传失败,"+data.res.msg);
        },
        deleteImage:function () {
            $backPhoto.val("");
        }
    });


baseInfoForm.submit.off('tap').on('tap',function(e){
	//给隐藏域添加agentId
	$agentId.val(agentId);
    //添加埋点
    var buryingKey=19404; 
    var buryingMsg='goldAgentH5'+window.location.pathname+' submit';
    var buryingUrl = "http://qd.innjia.com:8032/ch?key=" + buryingKey + "&url="+ (buryingMsg || "");
        $.get(buryingUrl);
	var $this=$(this);
    var checkCase=$this.hasClass("act-ago");
    var backPhotoResult=baseInfoForm.uploadImg({targetSelector:baseInfoForm.backPhoto,noticeMsg:'身份证背面'});
    var frontPhotoResult=baseInfoForm.uploadImg({targetSelector:baseInfoForm.frontPhoto,noticeMsg:'身份证前面'});
    var businessCardResult=baseInfoForm.uploadImg({targetSelector:baseInfoForm.businessCard,noticeMsg:'工牌'});
    var IdCardResult=baseInfoForm.testIdCard({targetSelector:baseInfoForm.IdCard,noticeMsg:'身份证号码',length:18});
    var agentNameResult=baseInfoForm.testText({targetSelector:baseInfoForm.agentName,noticeMsg:'姓名'});
    var storeResult=baseInfoForm.testText({targetSelector:baseInfoForm.store,noticeMsg:'门店全称'});
    var companyResult=baseInfoForm.testText({targetSelector:baseInfoForm.company,noticeMsg:'公司名字'});
    sendData=common.getSendDataByForm(baseInfoForm.form);
    if(!!checkCase&&!!agentNameResult&&!!IdCardResult&&!!businessCardResult&&!!frontPhotoResult&&!!backPhotoResult&&!!storeResult&&!!companyResult){
    	 $this.removeClass("act-ago").addClass("acted-yet");
    	baseInfoForm.formPost({
    		url:url,
    		sendData:sendData,
    		resSuccess:function (data) {
                actedStyle(); 
                baseInfoForm.initForm(baseInfoForm.form); 
                window.location.href='registerAuth.html';
            },
            resFailed:function (data) {
                actAgoStyle();
                
            }
    	})
    }else{
    	console.log("不准发送ajax");
    }
    e.preventDefault();
    return false;
});
function actedStyle () {
    baseInfoForm.submit.removeClass("act-ago").addClass("acted-yet");
}
function actAgoStyle () {
    baseInfoForm.submit.removeClass("acted-yet").addClass("act-ago");
}
var $skipRegister=$('#skip-register');
var $mask=$('#mask');
var $alert=$('#alert');
$skipRegister.off('tap').on('tap',function(e){
    $mask.removeClass('mask-hide').addClass('mask-show');
    $alert.removeClass('mask-hide').addClass('mask-show');
    $alert.children('.alert-sure').off('tap').on('tap',function(e){
        $mask.removeClass('mask-show').addClass('mask-hide');
        $alert.removeClass('mask-show').addClass('mask-hide');
        window.location.href="index.html";
        e.preventDefault();
        return false;
    });
    $alert.children('.alert-cancel').off('tap').on('tap',function(e){
        $mask.removeClass('mask-show').addClass('mask-hide');
        $alert.removeClass('mask-show').addClass('mask-hide');
        e.preventDefault();
        return false;
    });
    e.preventDefault();
    return false;
});